package com.haomiao.cloud.learnrxjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Bind(R.id.textView)
    TextView textView;
    @Bind(R.id.button)
    Button button;
    @Bind(R.id.button2)
    Button button2;
    @Bind(R.id.button3)
    Button button3;
    @Bind(R.id.button4)
    Button button4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
    }

    private Observable<String> getTitle(final String url) {
        return Observable.create(new OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext(url + "100");
                subscriber.onCompleted();
            }
        });
    }

    private Observable<List<String>> query() {
        final ArrayList<String> aa = new ArrayList();
        aa.add("123");
        aa.add("456");
        aa.add("第三方");
        return Observable.create(new OnSubscribe<List<String>>() {
            @Override
            public void call(Subscriber<? super List<String>> subscriber) {
                subscriber.onNext(aa);
                subscriber.onCompleted();
            }
        });
    }

    /**
     * 基础写法
     */
    private void base() {
        //1.创建被观察者
        Observable<String> observable = Observable.create(new OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("Hello, world!");
                subscriber.onCompleted();
            }
        });

        //2.创建订阅者
        Subscriber<String> mySubscriber = new Subscriber<String>() {
            @Override
            public void onNext(String s) {
                textView.setText("基础写法");
            }

            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
            }
        };

        //3.订阅
        observable.subscribe(mySubscriber);

    }


    /**
     * 简化写法
     */

    private void easy() {
        //1.创建被观察者
        Observable<String> myObservable = Observable.just("Hello, world!");

        //2.创建订阅者
        Action1<String> onNextAction = new Action1<String>() {
            @Override
            public void call(String s) {
                textView.setText("简化写法");
            }
        };

        //3.订阅
        myObservable.subscribe(onNextAction);

    }

    /**
     * 操作符（Operators）
     * map 事件转换
     */

    private void operatorsMap() {
        Observable.just("Hello, world!")
                .map(new Func1<String, Integer>() {
                    @Override
                    public Integer call(String s) {
                        return s.hashCode();
                    }
                })
                .map(new Func1<Integer, String>() {
                    @Override
                    public String call(Integer integer) {
                        return integer.toString();
                    }
                })
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        textView.setText("Map操作符：" + s);
                    }
                });
    }

    /**
     * 操作符（Operators）
     * flatmap
     * take
     * filter
     */

    private void operatorsFlatMap(){
        query()
                .flatMap(new Func1<List<String>, Observable<String>>() {         //遍历了query中的数组返回新的Observable
                    @Override
                    public Observable<String> call(List<String> strings) {
                        return Observable.from(strings);
                    }
                })
                .map(new Func1<String, String>() {
                    @Override
                    public String call(String s) {
                        return s+"!!!";
                    }
                })
//                .filter(new Func1<String, Boolean>() {        //选择器  返回true才能被订阅者接收
//                    @Override
//                    public Boolean call(String s) {
//                        return !s.equals("123100");
//                    }
//                })
//                .take(1)                                      //最多输出数量
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        Logger.init("Demo");
                        Logger.e(s);
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                base();
                break;
            case R.id.button2:
                easy();
                break;
            case R.id.button3:
                operatorsMap();
                break;
            case R.id.button4:
                operatorsFlatMap();
                break;
            default:
                break;
        }
    }
}
